#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Bootloader_Security_Access_0x27_test.py
@Time    :   2022/08/26 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from numpy import byte, bytes_, int16
from pytest_assume.plugin import assume
 
import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SecurityAccess_Seed_NRC = [([0x27],[0x7F,0x27,0x13]),             
                           ([0x27,0x01,0x15],[0x7F,0x27,0x13]),
                           ([0x27,0x02,0xFF],[0x7F,0x27,0x13]),
                           ([0x27,0x03,0x11],[0x7F,0x27,0x12]),
                           ([0x27,0x03],[0x7F,0x27,0x12]),       
                           ([0x27,0x05],[0x7F,0x27,0x12]),
                           ([0x27,0x07],[0x7F,0x27,0x12])]

SecurityAccess_Key_NRC = [([0x27],[0x7F,0x27,0x13]),              
                          ([0x27,0x02],[0x7F,0x27,0x13]),
                          ([0x27,0x02,0xFF],[0x7F,0x27,0x13]),
                          ([0x27,0x02,0x11,0x33,0x22],[0x7F,0x27,0x13]), 
                          ([0x27,0x03],[0x7F,0x27,0x12]),       
                          ([0x27,0x06],[0x7F,0x27,0x12]),
                          ([0x27,0x08],[0x7F,0x27,0x12])]


class Test_SecurityAccess_Phy(object):

     @allure.severity('normal')
     @allure.story('Tester Security Access')
     @allure.description('Test positive response of 0x27 seed in programming session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Seed_programming_Phy(self,action,Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 2, iStep, False)
          with assume: assert size == 6


     @allure.severity('minor')
     @allure.description('Test NRC of 0x27 seed in programming session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Seed_NRC_programming_Phy(self,action,Driver):
          seed = []
          # Test NRC37 of seed of 0x27(第一次KEY错误回35，第二次KEY错误回35，第三次KEY错误回36, 之后在10s内申请种子都是回37，过了10s申请种子才回种子，错误回35)
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 2, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x35], 0, iStep, False)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x35], 0, iStep, False)
  
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x36], 0, iStep, False)
          time.sleep(2)

          for i in range(4):
               iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
               iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x37], 0, iStep, False)
               iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x37], 0, iStep, False)
               time.sleep(2)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          with assume: assert size == 6
          for i in range(4):
               seed.append(uds_resp[2+i])
          
          key = Driver['CAN'].byd_calcKey(seed)
          send_key = [0x27,0x02] + key
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, [0x67,0x02], 0, iStep, False)



     ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SecurityAccess_Seed_NRC]
     @allure.severity('minor')
     @allure.description('Test NRC of 0x27 seed in program session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',SecurityAccess_Seed_NRC,ids=ids)
     def test_SecurityAccess_Seed_NRC12_13_programming_Phy(self,action,Driver,req,resp):
          #Test NRC13 and NRC12 of 0x27 seed
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)
    

     @allure.severity('normal')
     @allure.description('Test positive response of 0x27 key in program session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Key_programming_Phy(self,action,Driver):
          seed = []
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01,0x00,0x00,0x00,0x00], 2, iStep, True)
              

     @allure.severity('minor')
     @allure.description('Test NRC of 0x27 key in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Key_NRC_programming_Phy(self,action,Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x11,0x11,0x11,0x11], [0x7F,0x27,0x24], 2, iStep, False)
          iStep = Driver['CAN'].unlock(Driver,iStep)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01,0x00,0x00,0x00,0x00], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x11,0x11,0x11,0x11], [0x7F,0x27,0x24], 0, iStep, False)
          
          iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 2, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x11,0x12,0x13,0x14], [0x7F,0x27,0x35], 0, iStep, False)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x35], 0, iStep, False)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x36], 0, iStep, False)


          # Test NRC37 of key of 0x27
          for i in range(4):
               iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x37], 0, iStep, False)
               iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x37], 0, iStep, False)
               time.sleep(2)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x7F,0x27,0x37], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x37], 0, iStep, False)
  

  
          time.sleep(2)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 0, iStep, False)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x02,0x22,0x22,0x22,0x22], [0x7F,0x27,0x35], 0, iStep, False)


     ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SecurityAccess_Key_NRC]
     @allure.severity('minor')
     @allure.description('Test NRC of 0x27 key in program session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',SecurityAccess_Key_NRC,ids=ids)
     def test_SecurityAccess_Key_NRC12_13_programming_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 2, iStep, False)
          with assume: assert size == 6
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)

     
     @allure.severity('normal')
     @allure.story('Tester Security Access')
     @allure.description('Test positive response of 0x27 seed in programming session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Three_Seed_programming_Phy(self,action,Driver):
          iStep = 1
          time.sleep(0.1)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 2, iStep, False)
          seed1 = uds_resp[2:6]
          with assume: assert size == 6
          time.sleep(0.2)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          seed2 = uds_resp[2:6]
          with assume: assert size == 6
          time.sleep(0.2)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          seed3 = uds_resp[2:6]
          with assume: assert size == 6

          same1_2 = np.array_equal(seed1, seed2)
          same1_3 = np.array_equal(seed1, seed3)
          same2_3 = np.array_equal(seed2, seed3)

          with assume: assert same1_2 != True and same1_3 != True and same2_3 != True

     
     @allure.severity('normal')
     @allure.story('Tester Security Access')
     @allure.description('Test positive response of 0x27 seed in programming session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_Three_Seed_NRC35_programming_Phy(self,action,Driver):
          iStep = 1
          time.sleep(0.1)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 2, iStep, False)
          seed1 = uds_resp[2:6]
          with assume: assert size == 6
          time.sleep(0.2)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          seed2 = uds_resp[2:6]
          with assume: assert size == 6
          time.sleep(0.2)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          seed3 = uds_resp[2:6]
          with assume: assert size == 6
          
          key = Driver['CAN'].byd_calcKey(seed2)
          send_key = [0x27,0x02] + key
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, send_key, [0x7F,0x27,0x35], 0, iStep, False)

     
     @allure.severity('normal')
     @allure.story('Tester Security Access')
     @allure.description('Test positive response of 0x27 seed in programming session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SecurityAccess_changeSession_programming_Phy(self,action,Driver):
          iStep = 1
          time.sleep(0.1)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 2, iStep, True)
          time.sleep(0.1)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10,0x02], [0x50,0x02], 0, iStep, False)
          time.sleep(0.1)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x27,0x01], [0x67,0x01], 0, iStep, False)
          seed = uds_resp[2:6]
          with assume: assert size == 6
          with assume: assert seed != [0x00,0x00,0x00,0x00]
